package com.cndw.kungfu.model.player;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.cndw.asyncsync.SyncObject;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Friend;
import com.cndw.kungfu.domain.Pet;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.disciple.domain.DiscipleRein;
import com.cndw.kungfu.model.goods.MarketCenter;
import com.cndw.kungfu.model.goods.PlayerMarket;
import com.cndw.kungfu.model.operation.domain.PlayerFruit;
import com.cndw.kungfu.model.player.domain.Artifact;
import com.cndw.kungfu.model.player.domain.Office;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.model.task.TaskProgress;
import com.cndw.rpg.framework.ResponseMessage;

/**
 * 玩家扩展数据,尽量减少同步次数
 * 
 * @author fantadust
 * @date 2011-5-18 下午05:28:35
 */
public class PlayerDataInfo extends SyncObject implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1009677889001L;

	private final long playerId;

	private int syncDBTime;
	/**
	 * 申请帮派列表//不再保存这不太重要的东西
	 */
	private transient Map<Integer, Integer> applyUnion;
	/**
	 * 随机任务的领取
	 */
	private Map<Integer, Integer> cycleTaskMap = new HashMap<Integer, Integer>(8);
	/**
	 * 任务完成进展
	 */
	private Map<Integer, TaskProgress> taskProgress = new HashMap<Integer, TaskProgress>();
	/**
	 * 玩家嗑药的情况
	 */
	private Map<Integer, Integer> drugsMap = new HashMap<Integer, Integer>();
	/**
	 * 玩家嗑药加成情况
	 */
	private Map<Integer, Integer> drugsAddMap = new HashMap<Integer, Integer>();
	/**
	 * 玩家帮派技能
	 */
	private Map<Integer, Integer> unionSkillMap = new HashMap<Integer, Integer>();
	/**
	 * 招聘到的临时徒弟对象
	 */
	private Map<Integer, List<DiscipleRein>> reinBuffer = new HashMap<Integer, List<DiscipleRein>>(3);
	/**
	 * 玩家声望属性值
	 */
	private Map<Integer, Integer> prestigeMap = new HashMap<Integer, Integer>(3);

	/**
	 * 好友列表
	 */
	private transient Map<Long, Friend> friendsMap;
	/**
	 * 我的宠物列表
	 */
	private transient Map<Long, Pet> petsMap;
	/**
	 * 好友分组
	 */
	private String[] groupNames = { LanguageLib.get("friend.group.name") };
	/**
	 * 完成的任务id
	 */
	private int mainTaskId;
	/**
	 * 是否通过防沉迷
	 */
	private transient boolean passAddicted;
	/**
	 * 上次cacheId;
	 */
	private int lastCacheId;
	/**
	 * 上次的剧情id
	 */
	private int lastStoryId;
	/**
	 * 激活的宠物id
	 */
	private long petId;
	/**
	 * 夫妻id
	 */
	private long mlId;

	/**
	 * 上次聊天时间
	 */
	private transient int lastChatTime;
	/**
	 * 上次场景id,进入副本时候
	 */
	private transient int lastSceneId;
	/**
	 * 副本id
	 */
	private transient int undercityId;

	/**
	 * 当前进行的战斗
	 */
	private transient int battleId;
	/**
	 * 对xxx玩家提出申请切磋
	 */
	private transient long learnTargetId;
	/**
	 * 上次刺杀时间
	 */
	private transient int lastSlaughter;
	/**
	 * 摆摊对象
	 */
	private transient PlayerMarket market;
	/**
	 * 组队邀请人
	 */
	private transient Set<Long> teamInviteIds;
	/**
	 * 上次背包整理时间
	 */
	private transient int lastClassify;
	/**
	 * 黄钻等级
	 */
	private transient int yLevel;
	/**
	 * 是否年费
	 */
	private transient boolean yYear;
	/**
	 * 腾讯opkey
	 */
	private transient String openKey;
	/**
	 * 腾讯pf
	 */
	private transient String pf;
	/**
	 * 腾讯pfkey
	 */
	private transient String pfKey;
	/**
	 * 3366等级
	 */
	private transient int lv3;

	private transient boolean gmLogin;
	/**
	 * 经验加成
	 */
	private transient int expCoef = 0;
	/**
	 * 经验加成过期时间
	 */
	private transient int expTime = 0;
	/**
	 * 上次数值集合
	 */
	private Map<String, Integer> commIntData = new HashMap<String, Integer>();
	/**
	 * 每日维护的数据
	 */
	private PlayerEveryDay playerEveryDay;
	/**
	 * 玩家获得的称号
	 */
	private Set<Integer> titles;
	/**
	 * 玩家的麻布袋
	 */
	private Map<Integer, Integer> lotteryBag;
	/**
	 * 宠物兽魂物种
	 */
	private Map<Integer, Integer> petHunt;
	/**
	 * 宠物兽魂经验
	 */
	private Map<Integer, Integer> petHuntExp;
	/**
	 * 通过的副本
	 */
	private Set<Integer> passUnders;
	/**
	 * 我的仓库密码
	 */
	private String lockPass;
	/**
	 * 打开了密码
	 */
	private transient boolean openPass;
	/**
	 * 上次pve时间
	 */
	private transient int lastPve;
	/**
	 * 经验找回map
	 * <p>
	 * key=当天起始时间->value=当前等级,押镖,神兵阵,远古塞外,BOSS殷天正,BOSS天地黑熊,BOSS催玉环,帮派战,双倍打坐,答题
	 * </p>
	 */
	private Map<Integer, int[]> expRet = new HashMap<Integer, int[]>(3);
	/**
	 * 神树的果实
	 */
	private Map<Integer, PlayerFruit> fruitMap;

	private Artifact artifact;

	public PlayerDataInfo(long playerId) {
		this.playerId = playerId;
	}

	public int intDataGet(String key) {
		if (commIntData.containsKey(key)) {
			return commIntData.get(key);
		}
		return 0;
	}

	public void intDataSet(String key, int value) {
		commIntData.put(key, value);
	}

	public void intDataAlter(String key, int value) {
		commIntData.put(key, intDataGet(key) + value);
	}

	public void intDataRemove(String key) {
		commIntData.remove(key);
	}

	public int intDataBitAdd(String key, int checkType) {
		checkType = intDataGet(key) | checkType;
		intDataSet(key, checkType);
		return checkType;
	}

	public boolean intDataBitContains(String key,int checkType){
		return (intDataGet(key) & checkType)>0;
	}

	public int getSyncDBTime() {
		return syncDBTime;
	}

	public void setSyncDBTime(int syncDBTime) {
		this.syncDBTime = syncDBTime;
	}

	public int getApplyUnionSize() {
		if (applyUnion == null) {
			return 0;
		}
		return applyUnion.size();
	}

	public void removeApplyUnion(int unionId) {
		if (applyUnion == null) {
			return;
		}
		applyUnion.remove(unionId);
	}

	public Integer getApplyUnion(int unionId) {
		if (applyUnion == null) {
			return 0;
		}
		return applyUnion.get(unionId);
	}

	public Set<Integer> getApplyUnions() {
		if (applyUnion == null) {
			return null;
		}
		return applyUnion.keySet();
	}

	public void clearApplyUnion() {
		if (applyUnion == null) {
			return;
		}
		applyUnion.clear();
	}

	public synchronized void putApplyUnion(int unionId, int timestamp) {
		if (applyUnion == null) {
			applyUnion = new HashMap<Integer, Integer>(ConstantsModel.unionApplyCounts);
		}

		applyUnion.put(unionId, timestamp);
	}

	public Map<Integer, Integer> getCycleTaskMap() {
		return cycleTaskMap;
	}

	public int getLastChatTime() {
		return lastChatTime;
	}

	public void setLastChatTime(int lastChatTime) {
		this.lastChatTime = lastChatTime;
	}

	public int getLastSceneId() {
		return lastSceneId;
	}

	public void setLastSceneId(int lastSceneId) {
		this.lastSceneId = lastSceneId;
	}

	public long getPlayerId() {
		return playerId;
	}

	public int getUndercityId() {
		return undercityId;
	}

	public void setUndercityId(int undercityId) {
		this.undercityId = undercityId;
	}

	public int getBattleId() {
		return battleId;
	}

	public void setBattleId(int battleId) {
		this.battleId = battleId;
	}

	public long getLearnTargetId() {
		return learnTargetId;
	}

	public void setLearnTargetId(long learnTargetId) {
		this.learnTargetId = learnTargetId;
	}

	public int getMainTaskId() {
		return mainTaskId;
	}

	public void setMainTaskId(int mainTaskId) {
		this.mainTaskId = mainTaskId;
	}

	public int getLastSlaughter() {
		return lastSlaughter;
	}

	public void setLastSlaughter(int lastSlaughter) {
		this.lastSlaughter = lastSlaughter;
	}

	public PlayerMarket getMarket() {
		return market;
	}

	public PlayerMarket openMarket(String playerName, String marketName, String notice) {
		market = new PlayerMarket(playerId, playerName, marketName, notice);
		MarketCenter.add(playerId, market);
		return market;
	}

	public void closeMarket() {
		market.closeMarket();
		market = null;
		MarketCenter.remove(playerId);
	}

	public Map<Integer, TaskProgress> getTaskProgress() {
		return taskProgress;
	}

	public boolean isTeamInvite(long playerId) {
		if (teamInviteIds != null && teamInviteIds.contains(playerId)) {
			return true;
		}
		return false;
	}

	public void addTeamInvite(long playerId) {
		if (teamInviteIds == null) {
			synchronized (this) {
				if (teamInviteIds == null) {
					teamInviteIds = new HashSet<Long>();
				}
			}

		}
		teamInviteIds.add(playerId);
	}

	public void removeTeamInvite(long playerId) {
		if (teamInviteIds != null) {
			teamInviteIds.remove(playerId);
		}
	}

	public void clearTeamInvite() {
		teamInviteIds = null;
	}

	public int getLastClassify() {
		return lastClassify;
	}

	public void setLastClassify(int lastClassify) {
		this.lastClassify = lastClassify;
	}

	/**
	 * 获得每日数据
	 * 
	 * @return
	 */
	public PlayerEveryDay getEveryDay() {
		if (playerEveryDay == null || playerEveryDay.isNeedReset(playerId)) {
			synchronized (this) {
				playerEveryDay = new PlayerEveryDay(playerId);
			}
		}
		return playerEveryDay;
	}

	/**
	 * 获得丹药使用的次数
	 * 
	 * @param drugType
	 * @param drugStage
	 * @return
	 */
	public int getDrugNum(int key) {
		if (drugsMap.containsKey(key)) {
			return drugsMap.get(key);
		}
		return 0;
	}

	public int increaseDrugNum(int key, int num) {
		if (drugsMap.containsKey(key)) {
			return drugsMap.put(key, drugsMap.get(key) + num) + num;
		} else {
			drugsMap.put(key, num);
			return num;
		}
	}

	public Map<Integer, Integer> getUnionSkillMap() {
		return unionSkillMap;
	}

	public int getUnionSkillLevel(Integer key) {
		if (unionSkillMap.containsKey(key)) {
			return unionSkillMap.get(key);
		}
		return 0;
	}

	/**
	 * 增加丹药加成数值
	 * 
	 * @param drugType
	 * @param count
	 * @return
	 */
	public int increaseDrugsAdd(int drugType, int count) {
		if (drugsAddMap.containsKey(drugType)) {
			return drugsAddMap.put(drugType, drugsAddMap.get(drugType) + count) + count;
		} else {
			drugsAddMap.put(drugType, count);
			return count;
		}
	}

	public Map<Integer, Integer> getDrugsAddMap() {
		return drugsAddMap;
	}

	/**
	 * 获得玩家嗑药情况
	 * 
	 * @return
	 */
	public Map<Integer, Integer> getDrugsMap() {
		return drugsMap;
	}

	/**
	 * 玩家招聘到的临时徒弟缓存
	 * 
	 * @return
	 */
	public Map<Integer, List<DiscipleRein>> getReinBuffer() {
		return reinBuffer;
	}

	public boolean isPassAddicted() {
		return passAddicted;
	}

	public void setPassAddicted(boolean passAddicted) {
		this.passAddicted = passAddicted;
	}

	// ====================好友相关
	public void initFriendsMap(Collection<Friend> friends) {
		int size = friends == null ? 1 : friends.size();
		friendsMap = new HashMap<Long, Friend>(size);
		if (friends != null) {
			for (Friend e : friends) {
				friendsMap.put(e.getFriendId(), e);
			}
		}
	}

	public void addFriend(Friend e) {
		friendsMap.put(e.getFriendId(), e);
	}

	public boolean removeFriend(long friendId) {
		if (!friendsMap.containsKey(friendId)) {
			return false;
		}
		friendsMap.remove(friendId);
		return true;
	}

	public Collection<Friend> getFriends() {
		return friendsMap.values();
	}

	public Friend getFriend(long friendId) {
		return friendsMap.get(friendId);
	}

	public Set<Long> getFriendIds() {
		return friendsMap.keySet();
	}

	public boolean isFriend(long uid) {
		return friendsMap.containsKey(uid);
	}

	public void setLastTalk(long friendId) {
		Friend friend = getFriend(friendId);
		if (friend != null) {
			friend.setLastTalk(TimeProcesser.getUnixTime());
		}
	}

	public String[] getGroupNames() {
		return groupNames;
	}

	public void setGroupNames(String[] groupNames) {
		if (groupNames == null)
			groupNames = new String[0];
		this.groupNames = groupNames;
	}

	// ===========================

	public int alterPrestige(int sceneId, int alter) {
		if (prestigeMap.containsKey(sceneId)) {
			alter += prestigeMap.get(sceneId);
		}
		prestigeMap.put(sceneId, alter);
		return alter;
	}

	public int getPrestige(int sceneId) {
		if (prestigeMap.containsKey(sceneId)) {
			return prestigeMap.get(sceneId);
		}
		return 0;
	}

	public Collection<Integer> getPrestiges() {
		return prestigeMap.values();
	}

	public int getLastCacheId() {
		return lastCacheId;
	}

	public void setLastCacheId(int lastCacheId) {
		this.lastCacheId = lastCacheId;
	}

	public int getLastStoryId() {
		return lastStoryId;
	}

	public void setLastStoryId(int lastStoryId) {
		this.lastStoryId = lastStoryId;
	}

	public Set<Integer> getTitles() {
		if (titles == null) {
			titles = new HashSet<Integer>();
		}
		return titles;
	}

	public Map<Integer, Integer> getLotteryBag(boolean create) {
		if (create && lotteryBag == null) {
			lotteryBag = new HashMap<Integer, Integer>();
		}
		return lotteryBag;
	}

	/**
	 * 添加通过的副本
	 * 
	 * @param undercityId
	 */
	public void passUndersAdd(int undercityId) {
		if (passUnders == null) {
			passUnders = new HashSet<Integer>();
		}
		passUnders.add(undercityId);
	}

	public Set<Integer> getUnders() {
		return passUnders;
	}

	/**
	 * 是否通过此副本
	 * 
	 * @param undercityId
	 * @return
	 */
	public boolean isPassUnder(int undercityId) {
		if (passUnders == null) {
			return false;
		}
		return passUnders.contains(undercityId);
	}

	public Map<Long, Pet> getPetsMap() {
		if (petsMap == null) {
			synchronized (this) {
				petsMap = Context.getPetServer().getPetsMap(playerId);
				if (petsMap == null) {
					petsMap = new HashMap<Long, Pet>(3);
				}
			}
		}
		return petsMap;
	}

	public long getPetId() {
		return petId;
	}

	public void setPetId(long petId) {
		this.petId = petId;
	}

	public long getMlId() {
		return mlId;
	}

	public void setMlId(long mlId) {
		this.mlId = mlId;
	}

	public Pet getPet() {
		return getPet(petId);
	}

	public Pet getPet(long id) {
		if (id == 0) {
			return null;
		} else if (getPetsMap().containsKey(id)) {
			return getPetsMap().get(id);
		} else {
			return null;
		}
	}

	public int getYLevel() {
		return yLevel;
	}

	public void setYLevel(int yellowVipLevel) {
		this.yLevel = yellowVipLevel;
	}

	public boolean isYYear() {
		return yYear;
	}

	public void setYYear(boolean yYear) {
		this.yYear = yYear;
	}

	public String getOpenKey() {
		return openKey;
	}

	public void setOpenKey(String openKey) {
		this.openKey = openKey;
	}

	public String getPf() {
		return pf;
	}

	public void setPf(String pf) {
		this.pf = pf;
	}

	public String getPfKey() {
		return pfKey;
	}

	public void setPfKey(String pfKey) {
		this.pfKey = pfKey;
	}

	public int getLv3() {
		return lv3;
	}

	public void setLv3(int lv3) {
		this.lv3 = lv3;
	}

	public boolean isGmLogin() {
		return gmLogin;
	}

	public void setGmLogin(boolean gmLogin) {
		this.gmLogin = gmLogin;
	}

	public int getExpCoef() {
		return expCoef;
	}

	public void setExpCoef(int expCoef) {
		this.expCoef = expCoef;
	}

	public int getExpTime() {
		return expTime;
	}

	public void setExpTime(int expTime) {
		this.expTime = expTime;
	}

	public int getLastPve() {
		return lastPve;
	}

	public void setLastPve(int lastPve) {
		this.lastPve = lastPve;
	}

	public String getLockPass() {
		return lockPass;
	}

	public void setLockPass(String lockPass) {
		this.lockPass = lockPass;
	}

	public boolean isOpenPass() {
		return openPass;
	}

	public void setOpenPass(boolean openPass) {
		this.openPass = openPass;
	}

	/**
	 * getExpRetMap
	 * 
	 * @return Map
	 */
	public Map<Integer, int[]> getExpRetMap() {
		if (null != expRet) {
			return expRet;
		} else {
			expRet = new HashMap<Integer, int[]>(3);
			// LogUtil.getMain().debug("test init");
			// 初始化
			int today = TimeProcesser.getMorningTime();
			for (int i = 1; i <= 3; i++) {
				int[] arr = { 0, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 };
				arr[0] = Context.getPlayerServer().getByPlayerId(playerId).getLevel(); // 当前等级
				expRet.put(today - 86400 * i, arr);
			}
			return expRet;
		}
	}

	/**
	 * 获取某天经验找回情况
	 * 
	 * @param day
	 *            每天起始时间
	 * @return int[]
	 */
	public int[] getExpRetrieve(int day) {
		if (!getExpRetMap().containsKey(day)) {
			// 添加某天未完成活动情况
			int[] arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
			arr[0] = Context.getPlayerServer().getByPlayerId(playerId).getLevel(); // 当前等级
			getExpRetMap().put(day, arr);
		}

		// 移除过期数据
		if (getExpRetMap().size() > 4) {
			int today = TimeProcesser.getMorningTime();
			Iterator<Entry<Integer, int[]>> iterator = getExpRetMap().entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<Integer, int[]> entry = iterator.next();
				if (entry.getKey() < (today - 86400 * 3)) {
					iterator.remove();
				}
			}
		}
		return expRet.get(day);
	}

	/**
	 * 登录初始化每天经验找回状态值
	 */
	public void initExpRetMap(boolean isNewDay) {
		// 检测前3天的记录是否存在
		try {
			int today = TimeProcesser.getMorningTime();
			for (int i = 0; i <= 3; i++) {
				int time = today - i * 86400;
				if (getExpRetMap().containsKey(time)) {
					if (1 == i && isNewDay) {
						getExpRetrieve(time)[0] = Context.getPlayerServer().getByPlayerId(playerId).getLevel();
					}
					continue;
				}

				int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
				array[0] = Context.getPlayerServer().getByPlayerId(playerId).getLevel(); // 当前等级
				getExpRetMap().put(time, array);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 更新经验找回map值
	 * 
	 * @param day
	 * @param pos
	 * @param value
	 */
	public void updateExpRet(int day, int pos) {
		try {
			getExpRetrieve(day)[pos]++;
			getExpRetrieve(day)[0] = Context.getPlayerServer().getByPlayerId(playerId).getLevel();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 完成经验找回
	 * 
	 * @param day
	 * @param pos
	 */
	public void updateExpRetByComplete(int day, int pos) {
		if (!getExpRetMap().containsKey(day)) {
			return;
		}
		try {
			getExpRetMap().get(day)[pos] = 99;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * alterExpRet
	 * 
	 * @param pos
	 */
	public void alterExpRet(int pos) {
		updateExpRet(TimeProcesser.getMorningTime(), pos);
	}

	/**
	 * 检测交易是否锁定
	 * 
	 * @return
	 */
	public boolean isGoodsLock() {
		if (openPass) {
			return false;
		} else if (lockPass == null) {
			return false;
		}
		int unlockTime = intDataGet(ConstantsIntKey.passUnLock);
		if (unlockTime > 0 && unlockTime <= TimeProcesser.getUnixTime()) {
			lockPass = null;
			openPass = true;
			intDataRemove(ConstantsIntKey.passUnLock);
			return false;
		}
		return true;
	}

	public int huntGet(int indexId) {
		if (petHunt == null || !petHunt.containsKey(indexId)) {
			return 0;
		}
		return petHunt.get(indexId);
	}

	public void huntRemove(int indexId) {
		petHunt.remove(indexId);
		if (petHuntExp != null) {
			petHuntExp.remove(indexId);
		}
	}

	public int huntAdd(int id) {
		if (petHunt == null) {
			synchronized (this) {
				if (petHunt == null) {
					petHunt = new HashMap<Integer, Integer>();
				}
			}
		}
		synchronized (petHunt) {
			for (int i = 1; i <= ConstantsModel.petHuntNumMax; i++) {
				if (petHunt.containsKey(i) == false) {
					petHunt.put(i, id);
					return i;
				}
			}
		}
		return 0;
	}

	public void huntPut(int indexid, int id) {
		if (petHunt == null) {
			return;
		}
		petHunt.put(indexid, id);
	}

	public int huntSize() {
		if (petHunt == null) {
			return 0;
		} else {
			return petHunt.size();
		}
	}

	public int huntExpGet(int indexId) {
		if (petHuntExp == null || !petHuntExp.containsKey(indexId)) {
			return 0;
		}
		return petHuntExp.get(indexId);
	}

	public void huntExpSet(int indexId, int exp) {
		if (petHuntExp == null) {
			if (exp == 0) {
				return;
			}
			synchronized (this) {
				if (petHuntExp == null) {
					petHuntExp = new HashMap<Integer, Integer>();
				}
			}
		}
		if (exp == 0) {
			petHuntExp.remove(indexId);
		} else {
			petHuntExp.put(indexId, exp);
		}
	}

	public Map<Integer, Integer> huntAll() {
		return petHunt;
	}

	public Artifact getArtifact() {
		return artifact;
	}

	public Artifact getArtifactByInit(boolean init) {
		if (init && artifact == null) {
			synchronized (this) {
				if (artifact == null) {
					artifact = new Artifact();
				}
			}
		}
		return artifact;
	}

	public Map<Integer, PlayerFruit> getFruitMap(boolean init) {
		if (init) {
			if (fruitMap == null) {
				synchronized (this) {
					if (fruitMap == null) {
						fruitMap = Context.getOperationServer().initTreeFruit(playerId);
					}
				}
			}
			Context.getOperationServer().treeFruitGrow(playerId, fruitMap);
		}

		return fruitMap;
	}

	public void setFruitMap(Map<Integer, PlayerFruit> fruitMap) {
		this.fruitMap = fruitMap;
	}

	public void alterAchive(int alter) {
		if (alter == 0) {
			return;
		}

		int curAchv = intDataGet(ConstantsIntKey.achv);
		curAchv += alter;

		intDataSet(ConstantsIntKey.achv, curAchv);
		int curLv = intDataGet(ConstantsIntKey.office);
		int newLv = Office.getLvByAchive(curAchv);
		if (curLv == newLv) {
			return;
		}
		if (curLv < 24 && newLv >= 24) {// 记录开始扣除战功的时间
			intDataSet(ConstantsIntKey.deAc, TimeProcesser.getMorningTime());
		}
		intDataSet(ConstantsIntKey.office, newLv);
		LogServer.recordLog(Context.getPlayerServer().getByPlayerId(playerId), ConstantsLog.player, 30401, Office.getOne(newLv).getTitle(),
				intDataGet(ConstantsIntKey.achv), Office.getOne(newLv).getLevel());
		ResponseMessage message = ResponseMessage.chatMsg(LanguageLib.get(curLv < newLv ? "player.office.upgrade" : "player.office.down",
				LangUtil.makeHtml(Office.getOne(newLv).getColor(), Office.getOne(newLv).getTitle())));
		Context.getTcpServer().broadcastResMsg(message, getPlayerId());

	}

	public int getMaxSpirit() {
		int maxSpirit = intDataGet(ConstantsIntKey.smax);
		if (maxSpirit == 0) {
			for (Pet e : getPetsMap().values()) {
				if (e.getSpirit() > maxSpirit) {
					maxSpirit = e.getSpirit();
				}
			}
			if (maxSpirit > 0) {
				// intDataSet(ConstantsIntKey.smax, maxSpirit);
			}
		}
		return maxSpirit;
	}

	@Override
	public boolean syncDB() {
		return Context.getSyncServer().syncDB(this);
	}

}
